package com.gs.synedocket.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.gs.synedocket.R;


public class DocketView extends FrameLayout {

    private TextView tvContent;

    public DocketView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public DocketView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DocketView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DocketView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DocketView);
        String title = ta.getString(R.styleable.DocketView_docket_title_text);
        int bg = ta.getColor(R.styleable.DocketView_docket_content_bg, 0);
        int icon = ta.getResourceId(R.styleable.DocketView_docket_content_icon, 0);

        View view = LayoutInflater.from(context).inflate(R.layout.view_docket, this, false);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        tvContent = view.findViewById(R.id.tv_content);
        if (bg != 0) {
            tvContent.setBackgroundColor(bg);
        }
        if (icon != 0) {
            Drawable drawable = ContextCompat.getDrawable(context, icon);
            if (drawable != null) {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                tvContent.setCompoundDrawablePadding(64);
                tvContent.setCompoundDrawables(null, null, drawable, null);
            }
        }
        addView(view);
        ta.recycle();
    }


    public void setContent(String text) {
        tvContent.setText(text);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        tvContent.setOnClickListener(onClickListener);
    }

}
